<h1>Basic Types and Basic Value Literals</h1>

<p>
Types can be viewed as value templates,
and values can be viewed as type instances.
This article will introduce the built-in basic types
and their value literals in Go.
Composite types will not get introduced in this article.
</p>

<h3>Built-in Basic Types in Go</h3>

<div>
Go supports following built-in basic types:
<ul>
<li>
	one boolean built-in boolean type: <code>bool</code>.
</li>
<li>
	11 built-in integer numeric types:
	<code>int8</code>, <code>uint8</code>, <code>int16</code>,
	<code>uint16</code>, <code>int32</code>, <code>uint32</code>,
	<code>int64</code>, <code>uint64</code>, <code>int</code>, <code>uint</code>,
	and <code>uintptr</code>.
</li>
<li>
	two built-in floating-point numeric types: <code>float32</code> and <code>float64</code>.
</li>
<li>
	two built-in complex numeric types: <code>complex64</code> and <code>complex128</code>.
</li>
<li>
	one built-in string type: <code>string</code>.
</li>
</ul>

<p></p>

<p>
Each of the 17 built-in basic types belongs to one different kind of type in Go.
We can use the above built-in types in code without importing any packages,
though all the names of these types are non-exported identifiers.
</p>

<p>
15 of the 17 built-in basic types are numeric types.
Numeric types include integer types, floating-point types and complex types.
</p>

Go also support two built-in type aliases,
<ul>
<li>
	<code>byte</code> is a built-in alias of <code>uint8</code>.
	We can view <code>byte</code> and <code>uint8</code> as the same type.
</li>
<li>
	<code>rune</code> is a built-in alias of <code>int32</code>.
	We can view <code>rune</code> and <code>int32</code> as the same type.
</li>
</ul>

<p>
The integer types whose names starting with an <code>u</code> are unsigned types.
Values of unsigned types are always non-negative.
The number in the name of a type means how many binary bits
a value of the type will occupy in memory at run time.
For example, every value of the <code>uint8</code> occupies 8 bits in memory.
So the largest <code>uint8</code> value is <code>255</code> (2<sup>8</sup>-1),
the largest <code>int8</code> value is <code>127</code> (2<sup>7</sup>-1), and
the smallest <code>int8</code> value is <code>-128</code> (-2<sup>7</sup>).
</p>

<p>
If a value occupies <b>N</b> bits in memory, we say the size of the value is <b>N</b> bits.
The sizes of all values of a type are always the same, so value sizes are often called as type sizes.
</p>

<p>
We often measure the size of a value based on the number of bytes it occupies in memory.
One byte contains 8 bits.
So the size of the <code>uint32</code> type is four bytes.
</p>

<p>
The size of <code>uintptr</code>, <code>int</code> and <code>uint</code> values
n memory are implementation-specific.
Generally, The size of <code>int</code> and <code>uint</code> values
are 4 on 32-bit architectures, and 8 on 64-bit architectures.
The size of <code>uintptr</code> value must be large enough to
store the uninterpreted bits of any memory address.
</p>

<p>
The real and imaginary parts of a <code>complex64</code> value are both <code>float32</code> values,
and the real and imaginary parts of a <code>complex128</code> value are both <code>float64</code> values.
</p>

<p>
In memory, all floating-point numeric values in Go are stored in
<a href="https://en.wikipedia.org/wiki/IEEE_754">IEEE-754 format</a>.
</p>

<p>
A boolean value represents a truth.
There are only two possible boolean values in memory,
they are denoted by the two predeclared named constants,
<code>false</code> and <code>true</code>.
Name constants will be introduced in <a href="constants-and-variables.html">the next article</a>.
</p>

<p>
In logic, a string value denotes a piece of text.
In memory, a string value stores a sequence of bytes, which is the
UTF-8 encoding representation of the piece of text denoted by the string value.
We can learn more facts on strings from the article
<a href="string.html">strings in Go</a> later.
</p>

Although there is only one built-in type for each of boolean and string types,
we can define custom boolean and string types
for the built-in boolean and string types.
So there can be many boolean and string types.
The same is for any kinds of numeric types.
The following are some type declaration examples.
In these declarations, the word <code>type</code> is a keyword.

<pre class="line-numbers "><code class="language-go">/* Some type definition declarations */

// status and bool are two different types.
type status bool
// MyString and string are two different types.
type MyString string
// Id and uint64 are two different types.
type Id uint64
// real and float32 are two different types.
type real float32

/* Some type alias declarations */

// boolean and bool denote the same type.
type boolean = bool
// Text and string denote the same type.
type Text = string
// U8, uint8 and byte denote the same type.
type U8 = uint8
// char, rune and int32 denote the same type.
type char = rune
</code></pre>

<p>
We can call the custom <code>real</code> type defined above
and the built-in <code>float32</code> type both as float32 types.
Note, the second <b><i>float32</i></b> word
in the last sentence is a general reference,
whereas the first one is a specified reference.
Similarly, <code>MyString</code> and <code>string</code> are both string types,
<code>status</code> and <code>bool</code> are both bool types, etc.
</p>

<p>
We can learn more on custom types in the article
<a href="type-system-overview.html">overview of Go type system</a> later.
</p>

</div>

<h3>Zero Values</h3>

<div>
Each type has a zero value.
The zero value of a type can be viewed as the default value of the type.
<ul>
<li>
	The zero value of a boolean type is false.
</li>
<li>
	The zero value of a numeric type is zero, though zeros of different numeric types may have different sizes in memory.
</li>
<li>
	The zero value of a string type is an empty string.
</li>
</ul>
</div>

<a class="anchor" id="basic-value-literals"></a>
<h3>Basic Value Literals</h3>

<p>
A literal of a value is a text representation of the value in code.
A value may have many literals.
</p>

<p>
The literals denoting values of basic types are called basic value literal.
A basic value literal is also called a literal constant,
or an unnamed constant.
Named constants will be introduced in the next article.
</p>

<h4>Boolean value literals</h4>

<p>
Go specification doesn't define boolean literals.
However, in general programming, we can view the two predeclared identifiers,
<code>false</code> and <code>true</code>, as boolean literals.
But we should know that the two are not literals in the strict sense.
</p>

<p>
As mentioned above, zero values of boolean types
are denoted with the predeclared <code>false</code> constant.
</p>

<h4>Integer value literals</h4>

<div>
There are three integer value literal forms,
the decimal (base 10) form, the octal (base 8) form,
the hex (base 16) form and the binary form (base 2).
For example, the following three integer literals
all denote <code>15</code> in decimal.

<pre class="disable-line-numbers111"><code class="language-go">0xF // the hex form (starts with a "0x" or "0X")
0XF

017 // the octal form (starts with a "0", "0o" or "0O")
0o17
0O17

0b1111 // the binary form (starts with a "0b" or "0B")
0B1111

15 // the decimal form (starts without a "0")
</code></pre>

<p>
(Note: the binary form and the octal from starting with
<code>0o</code> or <code>0O</code> are supported since Go 1.13.)
</p>

The following program will print two <code>true</code> texts.
<pre class="line-numbers"><code class="language-go">package main

func main() {
	println(15 == 017) // true
	println(15 == 0xF) // true
}
</code></pre>
</div>

<p>
Note, the two <code>==</code> are the equal-to comparison operator,
which will be introduced in <a href="operators.html">common operators</a>.
</p>

<p>
Generally, zero values of integer types are denoted as <code>0</code>
in literal, though there are many other legal literals for integer zero values,
such as <code>00</code> and <code>0x0</code>.
In fact, the zero value literals introduced in the current article
for other kinds of numeric types can also
represent the zero value of any integer type.
</p>

<h4>Floating-point value literals</h4>

<div>
A floating-point value literal can contain a decimal integer part,
a decimal point, a decimal fractional part, and an integer exponent part.
Example (<code>xEn</code> is equivalent to <code>x</code> is multiplied by <code>10<sup>n</sup></code>,
and <code>xE-n</code> is equivalent to <code>x</code> is divided by <code>10<sup>n</sup></code>):
<pre class="disable-line-numbers111"><code class="language-go">1.23
01.23 // == 1.23
.23
1.
// A "e" or "E" starts the exponent part (10-based).
1.23e2  // == 123.0
123E2   // == 12300.0
123.E+2 // == 12300.0
1e-1    // == 0.1
.1e0    // == 0.1
0010e-2 // == 0.1
0e+5    // == 0.0
</code></pre>
</div>

Since Go 1.13, Go also supports another floating point literal form, hexadecimal floating point literal form.
<ul>
<li>
	Same as hex interger literals, a hexadecimal floating point literal also must start with <code>0x</code> or <code>0X</code>.
</li>
<li>
	Different from hex interger literals, letter <code>p</code> or <code>P</code>, which is followed by a 2-based exponent,  can appear in a hexadecimal floating point literal.
</li>
<li>
	Letter <code>e</code> and <code>E</code> can't appear in hexadecimal floating point literals.
</li>
</ul>

The followings are some valid hexadecimal floating point literals
(<code>yPn</code> is equivalent to <code>y</code> is multiplied by <code>2<sup>n</sup></code>,
and <code>yP-n</code> is equivalent to <code>y</code> is divided by <code>2<sup>n</sup></code>):
<pre class="disable-line-numbers111"><code class="language-go">0x1p-2     // == 0.25
0x2.p10    // == 2048.0
0x1.Fp+0   // == 1.9375
0X.8p-0    // == 0.5
0X1FFFP-16 // == 0.1249847412109375
</code></pre>

<p></p>

However, the following ones are invalid:
<pre class="disable-line-numbers111"><code class="language-go">0x.p1    // mantissa has no digits
1p-2     // p exponent requires hexadecimal mantissa
0x1.5e-2 // hexadecimal mantissa requires p exponent
</code></pre>

<p></p>

Note: the following literal is valid, but it is not a floating point literal.
It is a subtraction arithmetic expression actually.
The <code>e</code> in it means <code>14</code> in decimal.
<code>0x15e</code> is a hex interger literal, <code>-</code> is the subtraction operator,
and <code>2</code> is a decimal interger literal.
(Arithmetic operators will be introduced in the article <a href="operators.html">common operators</a>.)

<pre class="disable-line-numbers111"><code class="language-go">0x15e-2 // == 0x15e - 2 // a subtraction expression
</code></pre>

<p></p>

<p>
The standard literals for zero value of floating-point types are
<code>0.0</code>, though there are many other legal literals,
such as <code>0.</code>, <code>.0</code>, <code>0e0</code>, <code>0x0p0</code>, etc.
In fact, the zero value literals introduced in the current article
for other kinds of numeric types can also
represent the zero value of any floating-point type.
</p>

<h4>Imaginary value literals</h4>

<div>
An imaginary literal consists of a floating-point or integer literal
and a lower-case letter <code>i</code>.
Examples:
<pre class="disable-line-numbers111"><code class="language-go">1.23i
1.i
.23i
123i
0123i   // == 123i
1.23E2i // == 123i
1e-1i
</code></pre>

<p>
(Note: before Go 1.13, if the part before the lower-case letter <code>i</code> in an imaginary literal
is an integer literal, then it must be presented as the decimal form.)
</p>

Imaginary literals are used to represent the imaginary parts of complex values.
Here are some literals to denote complex values:
<pre class="disable-line-numbers111"><code class="language-go">1 + 2i       // == 1.0 + 2.0i
1. - .1i     // == 1.0 + -0.1i
1.23i - 7.89 // == -7.89 + 1.23i
1.23i        // == 0.0 + 1.23i
</code></pre>

<p></p>

<p>
The standard literals for zero values of complex types are
<code>0.0+0.0i</code>, though there are many other legal literals,
such as <code>0i</code>, <code>.0i</code>, <code>0+0i</code>, etc.
In fact, the zero value literals introduced in the current article
for other kinds of numeric types can also
represent the zero value of any complex type.
</p>

</div>

<a class="anchor" id="underscores-in-literals"></a>
<h4>Use <code>_</code> in numeric literals for better readability</h4>

<div>

Since Go 1.13, underscores <code>_</code> can appear in integer, floating-point
and maginary literals as digit separators to enhance code readability.
But please note, in a numeric literal,
<ul>
<li>
	any <code>_</code> is not allowed to
	be used as the first or the last character of the literal,
</li>
<li>
	the two sides of any <code>_</code> must be either literal prefixs
	(such as <code>0X</code>) or legal digit characters.
</li>
</ul>

Some valid and invalid numeric literals which contain underscores:

<pre class="disable-line-numbers111"><code class="language-go">// Valid ones:
6_9          // == 69
0_33_77_22   // == 0337722
0x_Bad_Face  // == 0xBadFace
0X_1F_FFP-16 // == 0X1FFFP-16
0b1011_0111 + 0xA_B.Fp2i

// Invalid ones:
_69        // _ can't appear as the first character
69_        // _ can't appear as the last character
6__9       // one side of _ is a illegal character
0_xBadFace // "x" is not a legal octal digit
1_.5       // "." is not a legal octal digit
1._5       // "." is not a legal octal digit
</code></pre>

<p></p>

</div>

<a class="anchor" id="rune"></a>
<h4>Rune value literals</h4>

<p>
Rune types, including custom defined rune types and
the built-in <code>rune</code> type (a.k.a., <code>int32</code> type),
are special integer types,
so all rune values can be denoted by the integer literals introduced above.
On the other hand, many values of all kinds of integer types can also be
represented by rune literals introduced below in the current subsection.
</p>

<p>
A rune value is intended to store a Unicode code point.
Generally, we can view a code point as a Unicode character,
but we should know that some Unicode characters are
composed of more than one code points each.
</p>

<div>
A rune literal is expressed as one or more characters enclosed in a pair of quotes.
The enclosed characters denote one Unicode code point value.
There are some minor variants of the rune literal form.
The most popular form of rune literals is just to enclose the characters denoted
by rune values between two single quotes. For example
<pre class="disable-line-numbers111"><code class="language-go">'a' // an English character
'π'
'众' // a Chinese character
</code></pre>

The following rune literal variants are equivalent to <code>'a'</code>
(the Unicode value of character <code>a</code> is 97).
<pre class="disable-line-numbers111"><code class="language-go">// 141 is the octal representation of decimal number 97.
'\141'
// 61 is the hex representation of decimal number 97.
'\x61'
'\u0061'
'\U00000061'
</code></pre>

<p>
Please note, <code>\</code> must be followed by exactly three octal digits to represent a byte value,
<code>\x</code> must be followed by exactly two hex digits to represent a byte value,
<code>\u</code> must be followed by exactly four hex digits to represent a rune value,
and <code>\U</code> must be followed by exactly eight hex digits to represent a rune value.
Each such octal or hex digit sequence must represent a legal Unicode code point, otherwise, it fails to compile.
</p>

The following program will print 7 <code>true</code> texts.
<pre class="line-numbers"><code class="language-go">package main

func main() {
	println('a' == 97)
	println('a' == '\141')
	println('a' == '\x61')
	println('a' == '\u0061')
	println('a' == '\U00000061')
	println(0x61 == '\x61')
	println('\u4f17' == '众')
}
</code></pre>

<p>
In fact, the four variant rune literal forms just mentioned
are rarely used for rune values in practice.
They are occasionally used in interpreted string literals
(see the next subsection for details).
</p>

If a rune literal is composed by two characters (not including the two quotes),
the first one is the character <code>\</code> and the second one is not
a digital character, <code>x</code>, <code>u</code> and <code>U</code>,
then the two successive characters
will be escaped as one special character.
The possible character pairs to be escaped are:
<pre class="disable-line-numbers111"><code class="language-go">\a   (Unicode value 0x07) alert or bell
\b   (Unicode value 0x08) backspace
\f   (Unicode value 0x0C) form feed
\n   (Unicode value 0x0A) line feed or newline
\r   (Unicode value 0x0D) carriage return
\t   (Unicode value 0x09) horizontal tab
\v   (Unicode value 0x0b) vertical tab
\\   (Unicode value 0x5c) backslash
\'   (Unicode value 0x27) single quote
</code></pre>

<p>
<code>\n</code> is the most used escape character pair.
</p>

An example:
<pre class="line-numbers"><code class="language-go">    println('\n') // 10
    println('\r') // 13
    println('\'') // 39

    println('\n' == 10)     // true
    println('\n' == '\x0A') // true
</code></pre>

<p>
There are many literals which can denote the zero values of rune types,
such as <code>'\000'</code>, <code>'\x00'</code>, <code>'\u0000'</code>, etc.
In fact, we can also use any numeric literal introduced above to represent
the values of rune types, such as <code>0</code>, <code>0x0</code>,
<code>0.0</code>, <code>0e0</code>, <code>0i</code>, etc.
</p>

</div>

<a class="anchor" id="string-literals"></a>
<h4>String value literals</h4>

<p>
String values in Go are UTF-8 encoded.
In fact, all Go source files must be UTF-8 encoding compatible.
</p>

<div>
There are two forms of string value literals,
interpreted string literal (double quote form)
and raw string literal (backquote form).
For example, the following two string literals are equivalent:

<pre class="disable-line-numbers111"><code class="language-go">// The interpreted form.
"Hello\nworld!\n\"你好世界\""

// The raw form.
`Hello
world!
"你好世界"`
</code></pre>

<p>
In the above interpreted string literal, each <code>\n</code> character pair
will be escaped as one newline character, and each <code>\"</code> character pair
will be escaped as one double quote character.
Most of such escape character pairs are the same as the escape character pairs
used in rune literals introduced above, except that <code>\"</code> is only
legal in interpreted string literals and <code>\`</code> is only legal in
rune literals.
</p>

The character sequence of <code>\</code>, <code>\x</code>,
<code>\u</code> and <code>\U</code> followed by several octal or hex digits
introduced in the last section can also be used in interpreted string literals.

<pre class="disable-line-numbers111"><code class="language-go">
// The following interpreted string literals are equivalent.
"\141\142\143"
"\x61\x62\x63"
"abc"

// The following interpreted string literals are equivalent.
"\u4f17\xe4\xba\xba"
      // The Unicode of 众 is 4f17, which is
      // UTF-8 encoded as three bytes: e4 bc 97.
"\xe4\xbc\x97\u4eba"
      // The Unicode of 人 is 4eba, which is
      // UTF-8 encoded as three bytes: e4 ba ba.
"\xe4\xbc\x97\xe4\xba\xba"
"众人"
</code></pre>

<p>
Please note that each English character (code point) is represented with one byte,
but each Chinese character (code point) is represented with three bytes.
</p>

<p>
In a raw string literal, no character sequences will be escaped.
The backquote character is not allowed to appear in a raw string literal.
To get better cross-platform compatibility, carriage return characters
(Unicode code point <code>0x0D</code>) inside raw string literals will be discarded.
</p>

<p>
Zero values of string types can be denoted as <code>""</code>
or <code>``</code> in literal.
</p>

</div>

<a class="anchor" id="representability"></a>
<h3>Representability of Basic Value Literals</h3>

<p>
Any one string literal can represent values of any string types.
</p>

<p>
The predeclared <code>false</code> and <code>true</code>
can represent values of any boolean types.
But again, we should know that the two are not literals in the strict sense.
</p>

<p>
A numeric literal can be used to represent as an integer value only if it needn't be rounded.
For example, <code>1.23e2</code> can represent as values of any basic integer types,
but <code>1.23</code> can't represent as values of any basic integer types.
Rounding is allowed when using a numeric literal to represent a non-integer basic numeric values.
</p>

<p>
Each basic numeric type has a representable value range.
So, if a literal overflows the value range of a type,
then the literal is not representable as values of the type.
</p>

Some examples:
<table border="1" class="table table-bordered text-center" style="width: auto !important;">
<thead>
	<tr class="active">
		<th class="text-center" valign="bottom" align="center" style="white-space: nowrap;">The Literal</th>
		<th class="text-center" valign="bottom" align="center">Types Which Values the Literal Can Represent</th>
	</tr>
</thead>
<tbody>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>256</code></td>
		<td style="vertical-align: middle;" class="text-left" valign="middle" align="left">
			All basic numeric types except int8 and uint8 types.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>255</code></td>
		<td style="vertical-align: middle;" class="text-left" valign="middle" align="left">
			All basic numeric types except int8 types.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>-123</code></td>
		<td style="vertical-align: middle;" class="text-left" valign="middle" align="left">
			All basic numeric types except the unsigned ones.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>123</code></td>
		<td style="vertical-align: middle;" class="text-left" rowspan="5" valign="middle" align="left">
			All basic numeric types.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>123.000</code></td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>1.23e2</code></td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>'a'</code></td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>1.0+0i</code></td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>1.23</code></td>
		<td style="vertical-align: middle;" class="text-left" rowspan="2" valign="middle" align="left">
			All basic floating-point and complex numeric types.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>0x10000000000000000</code><br/>(16 zeros)</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>3.5e38</code></td>
		<td style="vertical-align: middle;" class="text-left" valign="middle" align="left">
			All basic floating-point and complex numeric types except float32 and complex64 types.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>1+2i</code></td>
		<td style="vertical-align: middle;" class="text-left" valign="middle" align="left">
			All basic complex numeric types.
		</td>
	</tr>
	<tr>
		<td scope="row" style="vertical-align: middle; white-space: nowrap;" class="text-center" valign="middle" align="center"><code>2e+308</code></td>
		<td style="vertical-align: middle;" class="text-left" valign="middle" align="left">
			None basic types.
		</td>
	</tr>
</tbody>
</table>

<div class="note">
Notes:
<ul>
<li>
	Because no values of the basic integer types provided in Go can hold
	<code>0x10000000000000000</code>, so the literal is not
	representable as values of any basic integer types.
</li>
<li>
	The maximum IEEE-754 float32 value which can be represented accurately
	is <code>3.40282346638528859811704183484516925440e+38</code>,
	so <code>3.5e38</code> is not representable as values of
	any float32 and complex64 types.
</li>
<li>
	The max IEEE-754 float64 value which can be represented accurately
	is <code>1.797693134862315708145274237317043567981e+308</code>,
	so <code>2e+308</code> is not representable as
	values of any <code>float64</code> and <code>complex128</code> types.
</li>
<li>
	In the end, please note, although <code>0x10000000000000000</code>
	can represent values of float32 types,
	however it can't represent any float32 values accurately in memory.
	In other words, it will be rounded to the closest float32 value which can be
	represented accurately in memory when it is used as values of float32 types.
</li>
</ul>
</div>
